package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.vo.CheckGroupVo;
import org.apache.commons.lang3.StringUtils;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public  class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup>
        implements CheckGroupService {
    @Autowired
    private CheckItemService checkItemService;
    @Override
    public Boolean saveCheckGroup(CheckGroupDTO checkGroupDTO) {
        //完成检查组录入  自动生成主键,制定要注意
        boolean flag = saveOrUpdate(checkGroupDTO);
        Integer id = checkGroupDTO.getId();
        System.out.println(id+"------>");
        //判断得到的id是否存在
        if(StringUtils.isNotBlank(String.valueOf(id))){
            //id存在，进行修改操作
            //先删除第三张表中的id
            baseMapper.updateItemGroupById(id);
        }
        //得到检查组的主键
        Integer gid = checkGroupDTO.getId();
        System.out.println(gid+"-----");
        //得到勾选的检查项的所有id
        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
        if (checkitemIds!=null&&checkitemIds.length>0) {
            for (Integer checkitemId : checkitemIds) {
                baseMapper.addCheckGroup(gid,checkitemId);
            }
        }
        return flag;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //条件构造
        QueryWrapper<CheckGroup> queryWrapper= new QueryWrapper<>();
        //表示显示所有的未进行逻辑删除的数据
        queryWrapper.eq("is_delete",0);
        Page<CheckGroup> page =null;
        if (StringUtils.isBlank(queryPageBean.getQueryString())) {
            //查询条件是空
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),queryWrapper);

        }else{
            //有条件查询
            queryWrapper.like("HELPCODE",queryPageBean.getQueryString());
            //分页查询
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize()),queryWrapper);
        }
        //讲数据库查询的分页参数数据封装到PageResult中
        PageResult pageResult = new PageResult(page.getTotal(),page.getRecords());
        return pageResult;
    }

    @Override
    public boolean updateItemGroupById(int id) {
        //先删除中间表的信息
      baseMapper.updateItemGroupById(id);
        return removeById(id);
    }

    @Override
    public CheckGroupVo updateCheckGroupById(Integer id) {
        //查询所有的检查项信息
        List<CheckItem> list = checkItemService.list();
        //通过检查组主键查找对应的勾选检查项信息
        Integer [] listItemIds = baseMapper.findCheckGroupById(id);
        CheckGroupVo checkGroupVo = new CheckGroupVo();
        checkGroupVo.setCheckitemList(list);
        checkGroupVo.setCheckitemIds(listItemIds);
        return checkGroupVo;
    }
}



